﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Produce
{
    public partial class JiHua_MX : System.Web.UI.Page
    {
        private void ReBind()
        {
            this.SqlDataSource1.SelectCommand =
@"select [生产计划明细].[id],
[产品].[品名]+' '+[产品].[规格]+'('+[产品].[单位]+')' as [产品及规格],
[生产单位].[名称] as [生产单位名称],
[生产计划明细].[数量]
from [生产计划明细],[产品],[生产单位]
where [产品].[id]=[生产计划明细].[产品ID]
and [生产单位].[id]=[生产计划明细].[生产单位ID]
and [生产计划明细].[计划ID]=" + (string)this.ViewState["ID"];
            GridView1.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string id = Request.QueryString["ID"];
                string name=Request.QueryString["n"];
                if (string.IsNullOrEmpty(id))
                {
                    Response.End();
                }
                if (string.IsNullOrEmpty(name))
                {
                    name = "生产计划";
                }
                this.ViewState["ID"] = id;
                this.JiHuaMC.Text = name;
                this.ReBind();
            }
            this.Message.Text = "·准备就绪";

        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }


        protected void Button_Save_Click(object sender, EventArgs e)
        {
            float jhl = 0;
            float.TryParse(this.ChanLiang.Text, out jhl);
            if (jhl <= 0)
            {
                this.Message.Text = "·计划产量输入有误";
                return;
            }

            this.SqlDataSource3.UpdateCommand = "if not exists(select * from [生产计划明细] where [计划ID]="
                + (string)this.ViewState["ID"] + " and [生产单位ID]=" + this.DanWei.Text + " and [产品ID]="
                + this.ChanPin.Text + ") begin insert into [生产计划明细]([计划ID],[产品ID],[生产单位ID],[数量])values("
                + (string)this.ViewState["ID"] + "," + this.ChanPin.Text + "," + this.DanWei.Text + "," + jhl + ") end";
            //Response.Write(this.SqlDataSource3.UpdateCommand);
            if (this.SqlDataSource3.Update() == 1)
            {
                this.Message.Text = "·增加成功";
                this.ReBind();
            }
            else
            {
                this.Message.Text = "·该记录已经存在，增加失败";
            }


        }

        protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            this.ReBind();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.ReBind();
        }

        protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            this.ReBind();
        }

        protected void GridView1_Sorted(object sender, EventArgs e)
        {
            this.ReBind();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.ReBind();
        }

        protected void Button_Finish_Click(object sender, EventArgs e)
        {
            Response.Redirect("JiHua.aspx");
        }
    }
}
